package org.cst8277.ums.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.cst8277.ums.entity.UserRole;
import org.cst8277.ums.mapper.UserRoleMapper;
import org.cst8277.ums.service.IUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * <p>
 * 用户角色关系表 服务实现类
 * </p>
 *
 * @author houbin
 * @since 2023-10-11
 */
@Service
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRole> implements IUserRoleService {
    @Autowired
    private UserRoleMapper userRoleMapper;

    @Override
    public UserRole getByUserId(long userId) {
        QueryWrapper<UserRole> wrapper = new QueryWrapper<>();
        //这里填写的是数据库字段，不是java属性
        wrapper.eq("user_id", userId);
        wrapper.orderByAsc("id");
        List<UserRole> userRoleList = userRoleMapper.selectList(wrapper);
        if (CollectionUtils.isEmpty(userRoleList)) {
            return null;
        } else {
            return userRoleList.get(0);
        }
    }
}
